<template>
  <div class="detail-page">
    <van-nav-bar
      left-text="返回"
      @click-left="$router.back()"
      fixed
      title="面经详情"
    />
    <header class="header">
      <h1>{{ articleDetail.stem }}</h1>
      <p>
        创建时间 | {{ articleDetail.createdAt }} 浏览量 |
        {{ articleDetail.views }} 点赞数 {{ articleDetail.likeCount }}
      </p>
      <p>
        <img :src="articleDetail.avatar" alt="" />
        <span>{{ articleDetail.creator }}</span>
      </p>
    </header>
    <main class="body" v-html="articleDetail.content"></main>
    <div class="opt">
      <van-icon
        :class="{ active: articleDetail.likeFlag === 1 }"
        name="like-o"
        @click="
          onChangeOpt(
            1,
            'likeFlag',
            '点赞',
            articleDetail.likeFlag === 1 ? false : true
          )
        "
      />
      <van-icon
        :class="{ active: articleDetail.collectFlag === 1 }"
        name="star-o"
        @click="
          onChangeOpt(
            2,
            'collectFlag',
            '收藏',
            articleDetail.collectFlag === 1 ? false : true
          )
        "
      />
    </div>
  </div>
</template>
  <script>
import { getDetailByIdAPI , updateDetailAPI } from "@/api/artical";
export default {
  data() {
    return {
      articleDetail: {},
    };
  },
  async created() {
    // 获取 url 上的参数
    // console.log(this.$route.params.id);
    const result = await getDetailByIdAPI(this.$route.params.id);
    console.log(result);
    this.articleDetail = result.data.data;
  },
  methods: {
    async onChangeOpt(optType, key, text, flag) {
      await updateDetailAPI(this.$route.params.id, optType);

      if (flag) {
        this.$toast(text + "成功");
        this.articleDetail[key] = 1; //高亮
      } else {
        this.$toast("取消" + text);
        this.articleDetail[key] = 0; //高亮
      }
    },
  },
};
</script>

  <style lang="less" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;
  .header {
    h1 {
      font-size: 24px;
    }
    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;
    > .van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;
      &.active {
        background: #fec635;
        color: #fff;
      }
    }
  }
}
</style>